<template>
  <div>
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>{{$route.meta.title}}</el-breadcrumb-item>
    </el-breadcrumb>-->
    <el-dialog :title="user.isAdd?'商品分类列表':'商品分类修改'" :visible.sync="user.isshow" @closed="close">
      {{form}}
      <el-form :model="form">
        <el-form-item label="上级菜单" label-width="120px">
          <el-select v-model="form.pid" placeholder="请选择上级菜单">
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option v-for="item in list" :key="item.id" :label="item.catename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <div class="my-upload">
            <h3 class="my-plus">+</h3>
            <img v-if="imgUrl" class="my-img" :src="imgUrl" alt />
            <input type="file" @change="changeFile" />
          </div>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取消</el-button>
        <el-button v-if="user.isAdd" @click="add">添加</el-button>
        <el-button type="primary" v-else @click="edit">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { indexRouters } from "../../../router";
import { mapGetters, mapActions } from "vuex";
import {
  reqcateadd,
  reqmenuInfo,
  reqcateedit,
  reqcateinfo,
} from "../../../request/api";
import { successAlert } from "../../../utils/alert";
export default {
  props: ["user"],
  mounted() {
    // this.getOne();
  },
  data() {
    return {
      form: {
        pid: "",
        catename: "",
        img: null,
        status: 2,
      },
      // list: [],
      imgUrl: "",
    };
  },
  computed: {
    ...mapGetters({
      list: "shopCate/list",
    }),
  },
  methods: {
    ...mapActions({
      reqList: "shopCate/reqList",
    }),
    changeFile(e) {
      let file = e.target.files[0];

      // 判断文件类型
      let imgArr = [".jpg", ".jpeg", ".png", ".gif"];

      let name = file.name; // "1.1.1.1.1.jpg"
      let extname = name.slice(name.lastIndexOf("."));

      // let extname = path.extname(file.name);
      if (!imgArr.includes(extname)) {
        errorAlert("请上传正确的图片格式");
        return;
      }
      // 根据文件生成一个图片地址
      this.imgUrl = URL.createObjectURL(file);

      //给form.img
      this.form.img = file;
    },

    //   关闭是否清空form
    close() {
      if (!this.user.isAdd) {
        this.empty();
      }
    },

    //修改
    edit() {
      console.log(this.form);
      reqcateedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          //   取消form
          this.cancel();
          //   重新刷新数据
          this.reqList();
          //   修改后清空form
          this.empty();
        }
      });
    },

    //获取一条数据
    getOne(id) {
      reqcateinfo({
        id: id,
      }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.id = id;
        }
      });
    },

    //   取消后form消失
    cancel() {
      this.user.isshow = false;
    },

    // 点击添加后
    add() {
      console.log(this.form);
      reqcateadd(this.form).then((res) => {
        console.log(111);
        if (res.data.code === 200) {
          successAlert(res.data.msg);
          //   弹窗消失
          this.cancel();
          //   清空
          this.empty();
          //   刷新列表
          this.reqList();
          this.imgUrl = "";
        }
      });
    },

    empty() {
      this.form = {
        pid: "",
        catename: "",
        img: null,
        status: 2,
      };
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../../less/index.less";
.my-upload {
  width: 100px;
  height: 100px;
  border: 1px dashed @header-color;
  position: relative;
}
.my-plus {
  width: 100%;
  height: 100%;
  text-align: center;
  line-height: 100px;
  color: @parmary;
}
.my-img {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
}
.my-upload input {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
}
</style>